/**
 * @file sos.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_SOS != 0

#include <stdio.h>

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void sos_task(lv_task_t * task);
static void set_sos_status(sos_status_t sos_status);
static void sos_do_dailing_next(void);
static lv_obj_t * sos_no_contacts_or_network_create(lv_obj_t * activity_obj);
static void sos_no_contacts_or_network_prepare_destory(lv_obj_t * activity_obj);

/**********************
 *  STATIC VARIABLES
**********************/
static sos_t sos_variables;

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
lv_obj_t * sos_create(lv_obj_t * activity_obj)
{
    sos_variables.phone_id = 0;
    if((false == watch_modem_sim_present_check_req())
            || (MMI_MODEM_SIGNAL_BAR_0 == watch_modem_get_signal_bar_req())
            || (NULL == phonebook_get_phone_number(sos_variables.phone_id))) {
        activity_obj = sos_no_contacts_or_network_create(NULL);
        return activity_obj;
    }

    lv_watch_activity_ext_t activity_ext;
    memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
    activity_ext.actId = ACT_ID_SOS;
    activity_ext.create = sos_create;
    activity_ext.prepare_destory = NULL;
    activity_obj = lv_watch_creat_activity_obj(&activity_ext);
    LV_ASSERT_MEM(activity_obj);
    if(activity_obj == NULL) return NULL;

    set_sos_status(SOS_GIF_ON);
    lv_sos_ext_t * ext =
        lv_obj_allocate_ext_attr(activity_obj, sizeof(lv_sos_ext_t));
    LV_ASSERT_MEM(ext);
    if(ext == NULL) return NULL;
    ext->gif_info = gif_open(activity_obj, ANIM_SOS, 1);
    if(!ext->gif_info) {
        return activity_obj;
    }
    ext->gif_task_p = lv_task_create(sos_task, 100, LV_TASK_PRIO_MID, (void *)ext->gif_info);

    app_adaptor_send_sos_info_req();

    return activity_obj;
}

extern gif_info_t * power_on_gif;
lv_res_t sos_btn_action(lv_obj_t * btn)
{
    (void)btn;

    lv_obj_t * phone_obj = lv_watch_get_activity_obj(ACT_ID_PHONE);
    if(phone_obj) {
        /* don't process sos or shutdown */
        return LV_RES_OK;
    }

#if USE_NON_DIRECT_POWER_OFF
    if(true != watch_modem_sim_present_check_req()) {
        shutdown_create_btn_action(NULL,LV_EVENT_CLICKED);
    } else {
        lv_watch_go_home();
        lv_watch_png_cache_all_free();

        lv_obj_t * obj = sos_create(NULL);
        LV_ASSERT_MEM(obj);
        if(obj == NULL) return LV_RES_OK;
    }
#else
    if(1) {
        shutdown_create_btn_action(NULL,LV_EVENT_CLICKED);
    } else {
        lv_watch_go_home();
        lv_watch_png_cache_all_free();

        lv_obj_t * obj = sos_create(NULL);
        LV_ASSERT_MEM(obj);
        if(obj == NULL) return LV_RES_OK;
    }
#endif /* USE_NON_DIRECT_POWER_OFF */
    return LV_RES_OK;
}
static void sos_dialing_delay_task(lv_task_t * task)
{
    sos_phone_status_t status = (sos_phone_status_t)(uint32_t)task->user_data;;

    printf("sos_variables.status is %d\n", sos_variables.status);
    if(SOS_DIALING_ON == get_sos_status()) {
        printf("dailing status is %d\n", status);
        if(SOS_PHONE_DIAL_SUCCESS == status) {
            sos_destory();
        } else {
            sos_do_dailing_next();
        }
    }
}

void sos_phone_dailing_status_ind(sos_phone_status_t status)
{
    lv_task_once(lv_task_create(sos_dialing_delay_task, 10, LV_TASK_PRIO_MID, (void *)(unsigned long)status));
}
/**********************
 *   STATIC FUNCTIONS
 **********************/
static void sos_task(lv_task_t * task)
{
    gif_info_t * info = (gif_info_t *)task->user_data;

    if(info->once_done) {
        sos_destory();
        set_sos_status(SOS_DIALING_ON);
        if(NULL != phonebook_get_phone_number(sos_variables.phone_id)) {
            phone_voice_call_req(phonebook_get_phone_number(sos_variables.phone_id));
        } else {
            sos_no_contacts_or_network_create(NULL);
        }
    }
}

static void sos_do_dailing_next(void)
{
    char * number = NULL;
    sos_variables.phone_id++;
    printf("sos_variables.phone_id is %d\n", sos_variables.phone_id);
    number = phonebook_get_phone_number(sos_variables.phone_id);
    if(NULL != number) {
        printf("number is %s\n", number);
        phone_voice_call_req(number);
    } else {
        sos_variables.phone_id = 0;
        printf("sos_variables.phone_id is %d\n", sos_variables.phone_id);
        number = phonebook_get_phone_number(sos_variables.phone_id);
        if(NULL != number) {
            printf("number is %s\n", number);
            phone_voice_call_req(number);
        } else {
            printf("there is no contact in sos_do_dailing_next\n");
            sos_no_contacts_or_network_create(NULL);
        }
    }
}

void sos_destory(void)
{
    if(SOS_GIF_ON == get_sos_status()) {
        lv_watch_png_cache_all_free();
        lv_sos_ext_t * ext = lv_obj_get_ext_attr(lv_watch_get_activity_obj(ACT_ID_SOS));
        gif_close(ext->gif_info);
        ext->gif_info = NULL;
        lv_task_del(ext->gif_task_p);
        lv_obj_del(lv_watch_get_activity_obj(ACT_ID_SOS));
    }
    memset(&sos_variables, 0, sizeof(sos_t));
}

sos_status_t get_sos_status(void)
{
    return sos_variables.status;
}

static void set_sos_status(sos_status_t sos_status)
{
    sos_variables.status = sos_status;
}

static lv_obj_t * sos_no_contacts_or_network_create(lv_obj_t * activity_obj)
{
    if(NULL == activity_obj) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_NO_CONTACTS_OR_NETWORK;
        activity_ext.create = sos_no_contacts_or_network_create;
        activity_ext.prepare_destory = sos_no_contacts_or_network_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(NULL == activity_obj) {
            return NULL;
        }
    }

    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(NULL == obj) {
        return NULL;
    }

    lv_obj_t * label = lv_label_create(obj, NULL);
    if(1 == lv_lang_act()) {
        lv_obj_add_style(label, LV_OBJ_PART_MAIN, &lv_watch_font20);
    } else {
        lv_obj_add_style(label, LV_OBJ_PART_MAIN, &lv_watch_font20);
    }

    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_obj_set_click(label, false);
    lv_label_set_text_id(label, WATCH_TEXT_ID_NO_CONTACTS_OR_NETWORK);
    lv_obj_align(label, obj, LV_ALIGN_CENTER, 0, 0);

    return obj;
}

static void sos_no_contacts_or_network_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();
}

#endif /*USE_LV_WATCH_PHONE*/
